package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.bean.JsonResult;
import com.qf.pojo.GoodsCategory;
import com.qf.service.GoodsCategoryService;
import com.qf.util.RUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Mr.Shang
 * @version 1.0
 * @date 2021/9/22 16:42
 * @Description 商品小分类控制层
 */
@Controller
@RequestMapping("/goodsCategory")
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @RequestMapping("/findByPage")
    @ResponseBody
    public JsonResult findByPage(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit", required = false, defaultValue = "4") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsCategory> goods = goodsCategoryService.findAll();
        System.out.println(goods);
        PageInfo<GoodsCategory> pageInfo = new PageInfo<>(goods);
        JsonResult jsonResult = JsonResult.ok();
        jsonResult.setData(pageInfo.getList());
        jsonResult.setCount(pageInfo.getTotal());

        return jsonResult;
    }

    @RequestMapping("updateGoods")
    @ResponseBody
    public String updateUser(GoodsCategory goodsCategory) {
        goodsCategoryService.updateGoods(goodsCategory);
        return "success";
    }

    @RequestMapping("deleteGoodsById")
    @ResponseBody
    public String deleteById(Integer id) {
        goodsCategoryService.deleteById(id);
        return "success";
    }

    @RequestMapping("addGoods")
    public String addUser(GoodsCategory goodsCategory) {
        System.out.println(goodsCategory);
        goodsCategoryService.addGoods(goodsCategory);
        return "redirect:http://localhost:8080/goodsList.html";
    }

    @RequestMapping("findClassAll")
    @ResponseBody
    public List<String> findClassAll(){
        List<String> aClass = goodsCategoryService.findClass();
        System.out.println(aClass);
        return aClass;
    }
}
